1185E - Polycarp and Snakes - CodeForces Solution


brute force implementation *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
 
typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii; 
typedef vector<vii> wgraf;
typedef pair<int,ii> edge;
typedef vector <ll> vl;
typedef pair <ll, ll> LL;
typedef vector <LL> vll;
 
#define UNVISITED 0
#define VISITED 1
#define pb push_back
#define F first
#define S second
 
void SOLVE(){
  ll n, m, a, b;
  
  cin >> n >> m;

  char matriz[n+5][m+5], arma[n+5][m+5], mayor='a'-1;

  memset(arma, '.', sizeof arma);
  map <char, vii> letras;

  for (ll i=1; i<=n; i++){
    for (ll j=1; j<=m; j++){
      cin >> matriz[i][j];
      if (matriz[i][j]!='.') {
        letras[matriz[i][j]].pb({i, j});
      }
    }
  }

  for (auto i:letras){
    bool fil=true, col=true;
    ll f=i.S[0].F, c=i.S[0].S;

    for (auto j:i.S){
      if (j.F!=f) fil=false;
      if (j.S!=c) col=false;
      if (fil==false && col==false) {
        cout << "NO\n";
        return;
      }
      a=j.F;
      b=j.S;
    }

    if (fil==true){
      for (ll j=c; j<=b; j++) arma[f][j]=i.F;
    } else {
      for (ll j=f; j<=a; j++) arma[j][c]=i.F;
    }

    mayor=i.F;
  }

  for (ll i=1; i<=n; i++){
    for (ll j=1; j<=m; j++) {
      if (matriz[i][j]!=arma[i][j]){
        cout << "NO\n";
        return;
      }
    }
  }

  cout << "YES\n";

  cout << max(mayor+1-'a', 0) << "\n";

  for (char i='a'; i<mayor+1; i++){

    if (letras[i].size()!=0)cout << letras[i][0].F << " " << letras[i][0].S << " " << letras[i][letras[i].size()-1].F << " " << letras[i][letras[i].size()-1].S << "\n";
    else cout << a << " " << b << " " << a << " " << b << "\n";

  }

}
 
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while(t--){
    SOLVE();
  }
}


Comments

Submit
0 Comments
More Questions

383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence